/* -+- file  ./kf2/GUI/AddContactDialog.cs -+- */
/*
 * kf2 jabber client
 * ~~~~~~~~~~~~~~~~~
 *
 * Copyright (C) 2006-2007 Przemysław Sitek
 * 				<przemyslaw.sitek at wp.pl> 
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation; either version 2 of the
 * License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this program; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

using System;
using System.Collections.Generic;
using Habazie.kf2.Core;
using Habazie.kf2.Net;
using Habazie.kf2.Roster;

namespace Habazie.kf2.GUI
{
	/// <summary>
	/// 	Dialog to add contacts to roster
	/// </summary>
	public class AddContactDialog : Gtk.Dialog
	{
		protected Gtk.ComboBoxEntry m_Name;
		protected Gtk.Entry m_JID;
		protected Gtk.ComboBoxEntry m_Group;
		protected Habazie.kf2.GUI.AccountSelector m_Account;
		protected Gtk.Table table1;
		protected Gtk.CheckButton m_Subscribe;
		private Item m_Item = null;	// Temporary item
		
		public AddContactDialog(App app) : base ()
		{
			Stetic.Gui.Build(this, typeof(Habazie.kf2.GUI.AddContactDialog));
			
			m_Account = new AccountSelector ();
			m_Account.Connections = app.Connections;
			table1.Attach (m_Account, 1,2,0,1);
			m_Account.Changed += AccountChanged;
			m_Account.Show ();
			
			m_JID.FocusOutEvent += delegate {
				FetchUserInfo ();
			};
			
			m_Name = Gtk.ComboBoxEntry.NewText ();
			table1.Attach (m_Name, 1, 2, 3, 4);
			m_Name.Show ();
			
			m_Group = Gtk.ComboBoxEntry.NewText ();
			table1.Attach (m_Group, 1, 2, 4, 5);
			m_Group.Show ();

			m_Account.Active = 0;
		}
		
		
		/// <summary>
		/// 	Fetch user info
		/// </summary>
		public void FetchUserInfo ()
		{
			if (m_Item != null)
				return;
			Account acc = m_Account.Account;
			if (acc != null) {
				try {
					m_Item = acc.CreateTemporaryItem (m_JID.Text);
					m_Item.UpdateVCard ();
					m_Item.InformationChanged += FetchedUserInfo;
				} catch (jabber.JIDFormatException) {
				}
			}
		}
		
		
		/// <summary>
		/// 	Information was fetched
		/// </summary>
		/// <remarks>
		/// 	Fill Name field with fetched nickname.
		/// 	Insert nickname and full name into combo selection list
		/// </remarks>
		protected void FetchedUserInfo (object sender, EventArgs args)
		{
			Item i = sender as Item;
			
			if (i.VCard.Nickname != null) {
				m_Name.AppendText (i.VCard.Nickname);
				if (m_Name.Entry.Text.Length == 0) {
					m_Name.Entry.Text = i.VCard.Nickname;
					m_Name.Entry.SelectRegion (0, -1);
				}
			}
			if (i.VCard.FullName != null)
				m_Name.AppendText (i.VCard.FullName);
		}
		
		
		/// <summary>
		/// 	User has changed selected account
		/// </summary>
		/// <remarks>
		/// 	Update list of group names in "Group" combo box
		/// </remarks>
		protected virtual void AccountChanged (object sender, System.EventArgs e)
		{
			Account acc = m_Account.Account;
			if (acc != null && acc.IsAuthenticated) {
				List<string> groups = new List<string>  ();
				foreach (Item i in acc.GetRosterRef ())
					foreach (string g in i.Groups)
						if (! groups.Contains (g))
							groups.Add (g);

				groups.Sort ();
				((Gtk.ListStore)m_Group.Model).Clear ();
				foreach (string g in groups)
					m_Group.AppendText (g);
			} else {
				((Gtk.ListStore)m_Group.Model).Clear ();
			}
		}
		

		/// <summary>
		/// 	User clicked Cancel button
		/// </summary>
		protected virtual void OnCancelClicked(object sender, System.EventArgs e)
		{
			Destroy ();
		}


		/// <summary>
		/// 	User clicked Ok button
		/// </summary>
		protected virtual void OnAddClicked(object sender, System.EventArgs e)
		{
			Account acc = m_Account.Account;
			if (acc != null && acc.IsAuthenticated) {
				try {
					jabber.JID jid = new jabber.JID (m_JID.Text);
					string name = m_Name.Entry.Text;
					string group = m_Group.Entry.Text;
					
					acc.GetRosterRef ().RequestAddItem (jid, name, group, m_Subscribe.Active);
					Destroy ();
				} catch (jabber.JIDFormatException) {
					Gtk.MessageDialog win = new Gtk.MessageDialog (this, Gtk.DialogFlags.Modal, Gtk.MessageType.Error,
								Gtk.ButtonsType.Ok, true, "<big><b>Bad Jabber ID</b></big>\n\nJabberID entered is not valid.");
					win.Run ();
					win.Destroy ();
				}
			} else {
				Gtk.MessageDialog win = new Gtk.MessageDialog (this, Gtk.DialogFlags.Modal, Gtk.MessageType.Error,
								Gtk.ButtonsType.Ok, true, "<big><b>Not connected</b></big>\n\nYou are not connected to a Jabber server.");
				win.Run ();
				win.Destroy ();
			}
		}
	}
	
}
